package com.koron.css2.serviceManage.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;


public class JsonUtils {

    // 定义jackson对象
    private static ObjectMapper MAPPER = new ObjectMapper();
    
    {
        MAPPER.setSerializationInclusion(Include.ALWAYS);
    }

    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
    	try {
			String string = MAPPER.writeValueAsString(data);
			return string;
		} catch (Exception e) {
			throw new RuntimeException("对象转JSON字符串异常", e);
		}
    }
    
    public static JsonNode stringToJsonNode(String data) {
    	try {
    		JsonNode jsonNode = MAPPER.readTree(data);
			return jsonNode;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    
    /**
     * 将json结果集转化为对象
     * 
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     * @throws Exception 
     */
    public static <T> T objectToPojo(Object jsonData, Class<T> beanType) {
    	try {
    		T t = MAPPER.readValue(MAPPER.writeValueAsString(jsonData), beanType);
            return t;
    	}catch(Exception ex) {
    		ex.printStackTrace();
    		throw new RuntimeException("数据转换异常", ex);
    	}
    }

	/**
	 * 将json结果集转化为对象
	 *
	 * @param jsonData json数据
	 * @param beanType 对象中的object类型
	 * @return
	 * @throws Exception
	 */
	public static <T> T objectToPojoIgnoreNone(Object jsonData, Class<T> beanType) {
		try {
			MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			T t = MAPPER.readValue(MAPPER.writeValueAsString(jsonData), beanType);
			MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
			return t;
		}catch(Exception ex) {
			throw new RuntimeException("数据转换异常", ex);
		}
	}

	/**
	 * 将json结果集转化为对象(对象为空字符串时也可以转换)
	 *
	 * @param jsonData json数据
	 * @param beanType 对象中的object类型
	 * @return
	 * @throws Exception
	 */
	public static <T> T objectToPojoIgnoreEmptyString(Object jsonData, Class<T> beanType) {
		try {
			MAPPER.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
			T t = MAPPER.readValue(MAPPER.writeValueAsString(jsonData), beanType);
			MAPPER.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, false);
			return t;
		}catch(Exception ex) {
			throw new RuntimeException("数据转换异常", ex);
		}
	}
    
    /**
     * 将json结果集转化为对象
     * 
     * @param jsonData json数据
     * @param clazz 对象中的object类型
     * @return
     * @throws Exception 
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType){
    	try {
    		T t = MAPPER.readValue(jsonData, beanType);
            return t;
    	}catch(Exception ex) {
    		throw new RuntimeException("数据转换异常", ex);
    	}
    }

	/**
	 * 将json结果集转化为对象,忽略json中多出的字段
	 *
	 * @param jsonData
	 *            json数据
	 * @param clazz
	 *            对象中的object类型
	 * @return
	 * @throws Exception
	 */
	public static <T> T jsonToPojoIgnoreNone(String jsonData, Class<T> beanType) {
		try {
			MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			T t = MAPPER.readValue(jsonData, beanType);
			MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
			return t;
		} catch (Exception ex) {
			throw new RuntimeException("数据转换异常", ex);
		}
	}

	/**
	 * 将json数据转换成pojo对象list
	 * <p>Title: jsonToList</p>
	 * <p>Description: </p>
	 * @param jsonData
	 * @return
	 */
	public static <T>List<T> jsonToList(String jsonData) {
//    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
		try {
			List<T> list = MAPPER.readValue(jsonData, List.class);
			return list;
		} catch (Exception e) {
			throw new RuntimeException("数据转换出错!!!", e);
		}
	}

    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		List<T> list = MAPPER.readValue(jsonData, javaType);
    		return list;
		} catch (Exception e) {
			throw new RuntimeException("数据转换出错!!!", e);
		}
    }
    
    public static <T>List<T> jsonToListIgnoreNone(String jsonData, Class<T> beanType) {
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    		List<T> list = MAPPER.readValue(jsonData, javaType);
    		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    		return list;
		} catch (Exception e) {
			throw new RuntimeException("数据转换出错!!!", e);
		}
    }
    
    /**
     * 将object数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> objectToList(Object jsonData, Class<T> beanType) {
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    		List<T> list = MAPPER.readValue(MAPPER.writeValueAsString(jsonData), javaType);
    		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    		return list;
		} catch (Exception e) {
			throw new RuntimeException("数据转换出错!!!", e);
		}
    }

    public static <T>List<T> objectToListIgnoreNone(Object jsonData, Class<T> beanType) {
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    		List<T> list = MAPPER.readValue(MAPPER.writeValueAsString(jsonData), javaType);
    		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    		return list;
		} catch (Exception e) {
			throw new RuntimeException("数据转换出错!!!", e);
		}
    }
    
}
